﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class BaoCaoDAO
    Public Sub LoadCombobox(ByVal cbx As ComboBox, ByVal sql As String)
        Dim load As DataProvider = New DataProvider
        cbx.DataSource = load.Sql_ExecuteTable(sql)
    End Sub
    Public Sub LoadGridView(ByVal dgv As DataGridView, ByVal sql As String)
        Dim load As DataProvider = New DataProvider
        dgv.DataSource = load.Sql_ExecuteTable(sql)
    End Sub
    Public Function ThemBaoCao(ByVal bcInsert As BaoCaoDTO) As Boolean
        Dim Insert As DataProvider = New DataProvider
        Dim sql As String = String.Format("Insert_BaoCao")
        Dim pr As SqlParameter() = {New SqlParameter("@MaBaoCao", bcInsert.MaBaoCao),
                                    New SqlParameter("@TenBaoCao", bcInsert.TenBaoCao),
                                    New SqlParameter("@MaLop", bcInsert.MaLop),
                                    New SqlParameter("@MaHocKy", bcInsert.MaHocKy),
                                    New SqlParameter("@MaMonHoc", bcInsert.MaMonHoc),
                                    New SqlParameter("@SoLuongDat", bcInsert.SoLuongDat),
                                    New SqlParameter("@TyLe", bcInsert.TyLe)}
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        For i As Integer = 0 To pr.Count() - 1
            cmd.Parameters.Add(pr(i))
        Next
        Return Insert.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function XoaBaoCao(ByVal id As String) As Boolean
        Dim Remove As DataProvider = New DataProvider
        Dim sql As String = String.Format("Remove_BaoCao")
        Dim pr As SqlParameter = New SqlParameter("@MaBaoCao", id)
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add(pr)
        Return Remove.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function SuaBaoCao(ByVal bcUpdate As BaoCaoDTO) As Boolean
        Dim Update As DataProvider = New DataProvider
        Dim sql As String = String.Format("Update_BaoCao")
        Dim pr As SqlParameter() = {New SqlParameter("@MaBaoCao", bcUpdate.MaBaoCao),
                                    New SqlParameter("@TenBaoCao", bcUpdate.TenBaoCao),
                                    New SqlParameter("@MaLop", bcUpdate.MaLop),
                                    New SqlParameter("@MaHocKy", bcUpdate.MaHocKy),
                                    New SqlParameter("@MaMonHoc", bcUpdate.MaMonHoc),
                                    New SqlParameter("@SoLuongDat", bcUpdate.SoLuongDat),
                                    New SqlParameter("@TyLe", bcUpdate.TyLe)}
        Dim cmd As SqlCommand = New SqlCommand(sql)
        cmd.CommandType = CommandType.StoredProcedure
        For i As Integer = 0 To pr.Count() - 1
            cmd.Parameters.Add(pr(i))
        Next
        Return Update.SQL_ExecuteNonQuery(cmd)
    End Function
    Public Function GetList() As BindingList(Of BaoCaoDTO)
        Dim _Listbaocao As New BindingList(Of BaoCaoDTO)
        Dim GetDT As DataProvider = New DataProvider
        Dim sql As String = "select MaBaoCao, TenBaoCao, SoLuongDat, TyLe, MaMonHoc, MaLop,MaHocKy  from BAOCAO"
        Dim rd As SqlDataReader
        rd = GetDT.SQL_ExcuteQueryReader(sql)
        While rd.Read()
            Dim item As New BaoCaoDTO()
            item.MaBaoCao = rd.GetString(0)
            item.TenBaoCao = rd.GetString(1)
            item.SoLuongDat = rd.GetInt32(2)
            item.TyLe = rd.GetDouble(3)
            item.MaMonHoc = rd.GetString(4)
            item.MaLop = rd.GetString(5)
            item.MaHocKy = rd.GetString(6)
            _Listbaocao.Add(item)
        End While
        Return _Listbaocao
    End Function
End Class
